package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class ViewShopServlet extends HttpServlet {
    private static final String VIEW_SHOP = "/WEB-INF/jsp/catalog/Shop.jsp";
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        CatalogService service = new CatalogService();

        //将页面渲染需要的值存入session中
        List<Category> categoryList = service.getCategoryList();
        List<Product> productList = service.getAllProductList();
        List<Item> itemList = service.getAllItemList();
        System.out.println(productList);
        System.out.println(itemList);

        HttpSession session = request.getSession();
        session.setAttribute("categoryList", categoryList);
        session.setAttribute("productList" , productList);
        session.setAttribute("itemList", itemList);

        for (int i = 0; i < itemList.size(); i++) {
            System.out.println(itemList.get(i).getItemId() + itemList.get(i).getAttribute2());
        }

        request.getRequestDispatcher(VIEW_SHOP).forward(request, response);
    }
}
